/// <reference path="../test-types.ts"/>

import * as _ from 'lodash';
import tyAssert from '../utils/ty-assert';
import * as fs from 'fs';
import server from '../utils/server';
import * as make from '../utils/make';
import { TyE2eTestBrowser } from '../utils/ty-e2e-test-browser';
import settings from '../utils/settings';


let brA: TyE2eTestBrowser;
let brB: TyE2eTestBrowser;

let owen: Member;
let owensBrowser: TyE2eTestBrowser;
let strangersBrowser: TyE2eTestBrowser;

let idAddress: IdAddress;
let siteId: any;

const localHostname = 'comments-for-e2e-test-embdscid';
const embeddingOrigin = 'http://e2e-test-embdscid.localhost:8080';
const pageAaaSlug = 'emb-cmts-aaa.html';


describe(`embcom.create-site-forum-intro-tour.2br.ec  TyT6AKBR2044`, () => {

  it("initialize people", async () => {
    brA = new TyE2eTestBrowser(wdioBrowserA, 'brA');
    brB = new TyE2eTestBrowser(wdioBrowserB, 'brB');
    owensBrowser = brA;
    owen = make.memberOwenOwner();
  });

  it("import a site", async () => {
    const site: SiteData = make.forumOwnedByOwen('embftour', { title: "Emb Cmts Forum Tour Test" });
    site.meta.localHostname = localHostname;
    site.settings.allowEmbeddingFrom = embeddingOrigin;
    site.settings.showCategories = false;

    // TESTS_MISSING create a site from scratch. Break out & reuse code from 
    // embedded-comments-create-site-no-verif-email-admin-area-tour.2browsers.test.ts ?

    idAddress = await server.importSiteData(site);
    siteId = idAddress.id;
  });

  it("create an embedding page aaa", async () => {
    const dir = 'target';
    fs.writeFileSync(`${dir}/${pageAaaSlug}`, makeHtml());
  });


  function makeHtml(): string {
    return `
<html>
<head>
<title>Embedded comments E2E test</title>
</head>
<body>
<p>Embedded comments E2E test page, for emb cmts forum intro tour<br>
Ok to delete. The comments: ("long ago" generated by the admin js bundle [5AKBST03])
</p>

<script>talkyardCommentsServerUrl='${settings.scheme}://${localHostname}.localhost';</script>
<script async defer src="${settings.scheme}://${localHostname}.localhost/-/talkyard-comments.js"></script>
<div class="talkyard-comments" data-discussion-id="" style="margin-top: 45px;">

<p>/End of page.</p>
</body>
</html>`;
  }

  it("Owen goes to the topic list", async () => {
    await owensBrowser.go2(idAddress.origin);
    await owensBrowser.tour.runToursAlthoughE2eTest();
  });

  it("... logs in", async () => {
    await owensBrowser.complex.loginWithPasswordViaTopbar(owen);
  });

  it("An intro tour appears", async () => {
    await owensBrowser.tour.assertTourStarts(true);
  });

  it("... Owen views the tour", async () => {
    console.log('Step 1');
    await owensBrowser.tour.clickNextForStepNr(1);
    console.log('Step 2');
    await owensBrowser.tour.clickNextForStepNr(2);
    console.log('Step 3');
    await owensBrowser.tour.clickNextForStepNr(3);
    console.log('Step 4');
    await owensBrowser.tour.clickNextForStepNr(4);
    console.log('Step 5');
    await owensBrowser.waitAndClick('.esAvtrName_name');
  });

  it("... but skips last steps 6 and 7. So tour won't get marked as seen [TyT2ABKRT05]", async () => {
    // Noop.
  });

  it("Owen goes to the blog", async () => {
    await owensBrowser.go2(embeddingOrigin + '/' + pageAaaSlug);
  });

  it("... Owen needs to login??", async () => {
    await owensBrowser.complex.loginIfNeededViaMetabar(owen);
  });

  it("... adds a comment, so a page gets created", async () => {
    await owensBrowser.complex.replyToEmbeddingBlogPost("Text text text");
  });

  it("... then back to the topic list", async () => {
    await owensBrowser.go2(idAddress.origin);
  });

  it("The intro tour appears again (since he didn't finish watching it)", async () => {
    await owensBrowser.tour.assertTourStarts(true);
  });

  it("... Owen views the tour", async () => {
    console.log('Step 1');
    await owensBrowser.tour.clickNextForStepNr(1);
    console.log('Step 2');
    await owensBrowser.tour.clickNextForStepNr(2);
    console.log('Step 3');
    await owensBrowser.tour.clickNextForStepNr(3);
    console.log('Step 4');
    await owensBrowser.tour.clickNextForStepNr(4);
    console.log('Step 5');
    await owensBrowser.waitAndClick('.esAvtrName_name');
  });

  it("... all steps, this time", async () => {
    console.log('Step 6');
    await owensBrowser.tour.clickNextForStepNr(6);
  });

  it("Now the tour won't start again", async () => {
    await owensBrowser.refresh2();  // beacon-saves tourTipsSeen
    // Here, the tour would restart, but ...
    await owensBrowser.refresh2();  // reloads up-to-date tourTipsSeen
    await owensBrowser.tour.assertTourStarts(false);
  });

});

